//
//  CRCommendCell.swift
//  GZWeibo007
//
//  Created by jiwang on 15/12/26.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class CRCommendCell: UITableViewCell {

    var status : CZStatus? {
        didSet {
            topView.status = status
            
            contentLabel.text = status?.text
        }
    }

    //自动返回行高
    func rowHeight(status : CZStatus) -> CGFloat {
        
        //根据不同的模型计算行高 设置cell的status,cell会根据status里面的内容重新设置每个控件的内容
        self.status = status
        
        //设置后更新约束 重新布局
        layoutIfNeeded()
        
        let height = CGRectGetMaxY(contentLabel.frame)
        
        return height + 5
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareUI () {
        
        //添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        
        //顶部视图添加约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53), offset: CGPoint(x: 5, y: 5))
        //发表内容添加约束
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 5, y: 5))
        
        //约束文字的宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 5))
        
    }
    
    //MARK: - 懒加载
    ///顶部视图
    lazy var topView : CRCommendTopView = CRCommendTopView()
    
    ///微博内容
    lazy var contentLabel : UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        
        return label
    }()
}
